﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyBanHang_N3A
{
    public partial class FormNhapHang : Form
    {
        public FormNhapHang()
        {
            InitializeComponent();
        }
        public string MaNV;
        void LayMaSPTheoMaPD(String MaPD)
        {
            List<CTPhieuDatDTO> ds = new List<CTPhieuDatDTO>();
            CTPhieuDatBUS bus = new CTPhieuDatBUS();
            ds = bus.LayMaSPTheoMaPD(MaPD);
            clMaSP.DataSource = ds;
            clMaSP.DisplayMember = "MaSP";            
            clMaSP.ValueMember = "MaSP";

        }
        int LaySoLuongDatHang(string MaSP,string MaDH)
        {
            CTPhieuDatBUS bus = new CTPhieuDatBUS();
            return bus.LaySoLuongDatHang(MaSP,MaDH);
        }
        string LayTenNhanVien(string maNV)
        {
            List<NhanVienDTO> ds = new List<NhanVienDTO>();
            NhanVienBUS bus = new NhanVienBUS();
            ds = bus.LayThongTinNhanVien(MaNV);
            return ds[0].TenNV.ToString();
        }
        //void LayThongTinNhanVien()
        //{
        //    List<NhanVienDTO> ds = new List<NhanVienDTO>();
        //    NhanVienBUS bus = new NhanVienBUS();
        //    ds = bus.LayThongTinNhanVien();
        //    cmbMaNV.DataSource = ds;
        //    cmbMaNV.DisplayMember = "MaNV";
        //    cmbMaNV.ValueMember = "MaNV";
        //}
        string LayTenSanPham(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].TenSP.ToString();
        }
        int LayDonGia(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].DonGia;
        }
        int LayCVSP(string maSP)
        {
            List<SanPhamDTO> ds = new List<SanPhamDTO>();
            SanPhamBUS bus = new SanPhamBUS();
            ds = bus.LayThongTinSanPham(maSP);
            return ds[0].CV_SP;
        }
        void LoadDonHang(ref double tongtien, ref int tongcv)
        {
            tongtien = 0;
            tongcv = 0;
            for (int i = 0; i < GridViewSanPham.Rows.Count; i++)
            {
                if (GridViewSanPham.Rows[i].Cells[4].Value == null)
                    return;
                double dongia = Convert.ToDouble(LayDonGia(GridViewSanPham.Rows[i].Cells[1].Value.ToString()));
                int cvSP = Convert.ToInt32(LayCVSP(GridViewSanPham.Rows[i].Cells[1].Value.ToString()));
                double thanhtien = Convert.ToDouble(GridViewSanPham.Rows[i].Cells[3].Value) * dongia;
                int diem = Convert.ToInt32(GridViewSanPham.Rows[i].Cells[3].Value) * cvSP;
                GridViewSanPham.Rows[i].Cells[4].Value = dongia.ToString();
                GridViewSanPham.Rows[i].Cells[5].Value = thanhtien.ToString();
                tongtien += thanhtien;
                tongcv += diem;

            }

        }
        string KhoiTaoMaCTBH()
        {
            CTPhieuNhapBUS bus = new CTPhieuNhapBUS();
            return bus.TaoCTNHMoi();
        }
        string KhoiTaoMaBH()
        {
            PhieuNhapBUS bus = new PhieuNhapBUS();
            return bus.TaoMaPNMoi();
        }
        void LayMaPhieuDatChuaNhap()
        {
            List<PhieuDatDTO> ds = new List<PhieuDatDTO>();
            PhieuDatBUS bus = new PhieuDatBUS();
            ds = bus.LayMaPhieuDatChuaNhap();
            cmbMaPD.DataSource = ds;
            cmbMaPD.DisplayMember = "MaPD";
            cmbMaPD.ValueMember = "MaPD";
            cmbMaPD.SelectedValue = "";
            cmbMaPD.Text = "";
        }
        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        int ktTonTaiSP()
        {
            for (int i = 0; i < GridViewSanPham.RowCount - 2; i++)
                for (int j = i+1; j < GridViewSanPham.RowCount - 1; j++)
            {
                if (GridViewSanPham.Rows[i].Cells[1].Value.ToString() == GridViewSanPham.Rows[j].Cells[1].Value.ToString() )
                    return 1;// có tồn tại
            }
            return 0; // khong ton tai
        }

        private void FormNhapHang_Load(object sender, EventArgs e)
        {
            //LayThongTinNhanVien();
            lbTenNV.Text = LayTenNhanVien(MaNV);
            lbMaPN.Text = KhoiTaoMaBH().ToString();
            DateTime dh = DateTime.Now;
           // dtimeNgayNhap.Text = dh.ToString("dd/MM/yyyy");
           // dtimeNgayNhap.Value = dh.ToString("dd/MM/yyyy");
          //  LayThongTinSanPham();
            LayMaPhieuDatChuaNhap();
            //lây ma phieu dat


            this.GridViewSanPham.Rows.Clear();
        }

        private void btNhapHang_Click(object sender, EventArgs e)
        {
            if (cmbMaPD.SelectedIndex < 0)
            {
                MessageBox.Show("Bạn chưa Đặt Hàng");
                return;
            }
            PhieuNhapDTO pnDTO = new PhieuNhapDTO();
            PhieuNhapBUS bus = new PhieuNhapBUS();
            pnDTO.MaPN = lbMaPN.Text.Trim();
            pnDTO.MaPD = cmbMaPD.SelectedValue.ToString();
            pnDTO.NgayNH = dtimeNgayNhap.Value.Date;
            pnDTO.MaNV = MaNV.Trim();
            pnDTO.TongTien = Convert.ToDouble(lbTongTien.Text.ToString());
            int res = bus.ThemPhieuNhap(pnDTO);
            if (res == 1)
            {
                res = 0;
                for (int i = 0; i < this.GridViewSanPham.Rows.Count - 1; i++)
                {
                    CTPhieuNhapDTO ctpnDTO = new CTPhieuNhapDTO();
                    ctpnDTO.MaCTPN = KhoiTaoMaCTBH().ToString();
                    ctpnDTO.MaPD = cmbMaPD.SelectedValue.ToString();
                    ctpnDTO.MaPN = lbMaPN.Text.Trim();
                    ctpnDTO.MaSP = GridViewSanPham.Rows[i].Cells[1].Value.ToString();
                    ctpnDTO.SLNhap = Convert.ToInt32(GridViewSanPham.Rows[i].Cells[3].Value);
                    ctpnDTO.DonGia = Convert.ToDouble(GridViewSanPham.Rows[i].Cells[4].Value);
                    ctpnDTO.ThanhTien = Convert.ToDouble(GridViewSanPham.Rows[i].Cells[5].Value);
                    CTPhieuNhapBUS ctpnbus = new CTPhieuNhapBUS();
                    res = ctpnbus.ThemCTPhieuNhap(ctpnDTO);

                }
            }
            else
            {
                MessageBox.Show("Không Thành Công");
            }
            // cap nhat so luong cho san phap
            for (int i = 0; i < this.GridViewSanPham.Rows.Count - 1; i++)
            {
                SanPhamBUS spBUS = new SanPhamBUS();
                string MaSP = GridViewSanPham.Rows[i].Cells[1].Value.ToString();
                int SLTon = spBUS.LaySoLuongTon(MaSP);
                SLTon += Convert.ToInt32(GridViewSanPham.Rows[i].Cells[3].Value);
                spBUS.CapNhatSoLuongSanPham(SLTon, MaSP);
            }

            this.FormNhapHang_Load(sender, e);
        }

        private void GridViewSanPham_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            if (GridViewSanPham.Rows[e.RowIndex].Cells[1].Value == null)
                return;

            if (ktTonTaiSP() == 1)
            {
                MessageBox.Show("Sản Phẩm: " + GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString()+" đã có trong danh sach!");
                GridViewSanPham.Rows[e.RowIndex].Cells[1].Value = "";
                return;
            }

            string tenSP = LayTenSanPham(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString());

            int SLDat = LaySoLuongDatHang(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString(),cmbMaPD.SelectedValue.ToString()); // lay so lung dat, truyen maDH

            // kiem tra số luong chưa đặt
            if (SLDat <= 0) // vo nghia
            {
                MessageBox.Show("THÔNG BÁO!\r\n\n\t Sản Phẩm " + tenSP.ToString() + "\r\n\n\t\t Đã nhập rồi!");
                return;
            }

            if (tenSP == "")
                return;
            double dongia = Convert.ToDouble(LayDonGia(GridViewSanPham.Rows[e.RowIndex].Cells[1].Value.ToString()));
            
            this.GridViewSanPham.Rows[e.RowIndex].Cells[2].Value = tenSP;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[4].Value = dongia;
            string soluong = GridViewSanPham.Rows[e.RowIndex].Cells[3].Value.ToString(); //so luong nhap

            //Kiểm tra số lượng tồn trong kho và số lượng nhập vào

            if (Convert.ToInt32(soluong) > SLDat)
            {
                MessageBox.Show("Số lượng nhập tối đa là: " + SLDat.ToString());
                soluong = "0";
                GridViewSanPham.Rows[e.RowIndex].Cells[3].Value = 0;
                return;
            }
            this.GridViewSanPham.Rows[e.RowIndex].Cells[6].Value = SLDat - Convert.ToInt32(soluong); // lay so luong dat - luong nhap // ko co nghia

            double thanhtien = Convert.ToDouble(soluong) * dongia;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[5].Value = thanhtien;
            double tongtien = 0;
            for (int i = 0; i < this.GridViewSanPham.Rows.Count; i++)
            {
                tongtien += Convert.ToDouble(this.GridViewSanPham.Rows[i].Cells[5].Value);
            };
            lbTongTien.Text = tongtien.ToString();

        }

        private void GridViewSanPham_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            GridViewSanPham.Rows[e.RowIndex].Cells[3].Value = 0;
        }

        private void GridViewSanPham_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            this.GridViewSanPham.Rows[e.RowIndex].Cells[0].Value = e.RowIndex + 1;
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.GridViewSanPham.Rows.Clear();
            this.lbTongTien.Text = "0";
            DateTime dh = DateTime.Now;
            dtimeNgayNhap.Text = dh.ToString("dd/MM/yyyy");
        }

        private void cmbMaPD_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMaPD.SelectedIndex < 0)
                return;
            if (cmbMaPD.SelectedValue.ToString() == "DTO.PhieuDatDTO") 
            return;
            LayMaSPTheoMaPD(cmbMaPD.SelectedValue.ToString());
        }


        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Kim tra
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column3")
            {
                //Là các ký tự số hoặc điều khiển
                if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }
        private void txt_Validating(object sender, CancelEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Name == "Column3")
            {
                try
                {

                }
                catch
                {
                    MessageBox.Show("Bạn phải nhập vào dữ liệu kiểu số.");
                }
            }
        }

        private void GridViewSanPham_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.GridViewSanPham.CurrentCellAddress.X == GridViewSanPham.Columns[3].DisplayIndex)
            {
                TextBox txt = e.Control as TextBox;
                if (GridViewSanPham.RowCount < 0)
                    return;
                txt.Name = "Column3";
                if (txt != null)
                {
                    txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
                    txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
                    txt.Validating -= new CancelEventHandler(txt_Validating);
                    txt.Validating += new CancelEventHandler(txt_Validating);
                }
            }
        }

    }
}
